GLOBAL.DownloadMods = function (server_listing)
    local function enable_server_mods()
        print("We now have the required mods, enable them for server")
        for k,mod in pairs(server_listing.mods_description) do
            if mod.all_clients_require_mod then
                print("Temp Enabling " .. mod.mod_name)
                GLOBAL.KnownModIndex:TempEnable(mod.mod_name)
            end
        end

        local success, temp_config_data = GLOBAL.RunInSandboxSafe(server_listing.mods_config_data)
        if success and temp_config_data then
            GLOBAL.KnownModIndex:SetTempModConfigData( temp_config_data )
        end

        print("Mods are setup for server, save the mod index and proceed.")
        GLOBAL.KnownModIndex:Save()
    end

    local function server_listing_contains(mod_desc_table, mod_name )
        for _,mod in pairs(mod_desc_table) do
            if mod.mod_name == mod_name then
                return true
            end
        end
        return false
    end

    print("DownloadMods and temp disable")

    GLOBAL.KnownModIndex:UpdateModInfo()
    for _,mod_name in pairs(GLOBAL.KnownModIndex:GetServerModNames()) do
        local modinfo = GLOBAL.KnownModIndex:GetModInfo(mod_name)
        if not modinfo.client_only_mod then
            if server_listing_contains( server_listing.mods_description, mod_name ) then
                --we found it, so leave the mod enabled
            else
                --this mod is required by all clients but the server doesn't have it enabled or it's a server mod, so locally disable it temporarily.
                --print("Temp disabling ",mod_name)
                GLOBAL.KnownModIndex:TempDisable(mod_name)
            end
        end
    end
    if server_listing.client_mods_disabled then
		--temp disable all the client enabled mods
		for _,mod_name in pairs(GLOBAL.KnownModIndex:GetClientModNames()) do
			print("Temp disabling client mod", mod_name)
			GLOBAL.KnownModIndex:TempDisable(mod_name)
		end
    end
    GLOBAL.KnownModIndex:Save()

    if server_listing.mods_enabled then
        --verify that you have the same mods enabled as the server
        local have_required_mods = true
        local needed_mods_in_workshop = true
        local mod_count = 0
        for k,mod in pairs(server_listing.mods_description) do
            mod_count = mod_count + 1

            if Profile:GetAutoSubscribeModsEnabled() then
                GLOBAL.TheSim:SubscribeToMod(mod.mod_name)
            end

            if mod.all_clients_require_mod then
                if not GLOBAL.KnownModIndex:DoesModExist( mod.mod_name, mod.version ) then
                    print("Failed to find mod "..mod.mod_name.." v:"..mod.version )

                    have_required_mods = false
                    local can_dl_mod = GLOBAL.TheSim:QueueDownloadTempMod(mod.mod_name, mod.version)
                    if not can_dl_mod then
                        print("Unable to download mod " .. mod.mod_name .. " from ModWorkshop")
                        needed_mods_in_workshop = false
                    end
                end
            end
        end
        if mod_count == 0 then
            print("ERROR: Mods are enabled but the mods_description table has none in it?")
        end

        
        if have_required_mods then
            enable_server_mods()
            GLOBAL.TheNet:ServerModsDownloadCompleted(true, "", "")
        else
            if needed_mods_in_workshop then
                GLOBAL.TheSim:StartDownloadTempMods(
                    function( success, msg )
                        if success then
                            --downloading of mods succeeded, now double check if the right versions exists, if it doesn't then we downloaded the wrong version
                            local all_mods_good = true
                            local mod_with_invalid_version = nil
                            GLOBAL.KnownModIndex:UpdateModInfo() --Make sure we're verifying against the latest data in the mod folder
                            for k,mod in pairs(server_listing.mods_description) do
                                if mod.all_clients_require_mod then
                                    if not GLOBAL.KnownModIndex:DoesModExist( mod.mod_name, mod.version, mod.version_compatible ) then
                                        print("模组过期:", mod.modinfo_name, mod.mod_name, mod.version)
                                        all_mods_good = false
                                        mod_with_invalid_version = mod
                                    end
                                end
                            end

                            if all_mods_good then
                                enable_server_mods()
                                GLOBAL.TheNet:ServerModsDownloadCompleted(true, "", "")
                            else
                                local workshop_version = ""
                                if GLOBAL.KnownModIndex:GetModInfo(mod_with_invalid_version.mod_name) ~= nil then
                                    workshop_version = GLOBAL.KnownModIndex:GetModInfo(mod_with_invalid_version.mod_name).version
                                else
                                    print("ERROR: " .. (mod_with_invalid_version.mod_name or "") .. " has no modinfo, why???" )
                                end
                                if workshop_version == nil then
                                    workshop_version = ""
                                end
                                local version_mismatch_msg = "The server's version of " .. mod_with_invalid_version.modinfo_name .. " does not match the version on the Steam Workshop. Server version: " .. mod_with_invalid_version.version .. " Workshop version: " .. workshop_version
                                -- TheNet:ServerModsDownloadCompleted(false, version_mismatch_msg, "SERVER_MODS_WORKSHOP_VERSION_MISMATCH" )
                                -- TheNet:ServerModsDownloadCompleted(true, "", "")
                                local PDS = GLOBAL.require "screens/redux/popupdialog"
                                local content = "服务器的这个模组过期了：\n\n"
                                content = content..mod_with_invalid_version.modinfo_name.."    "..mod_with_invalid_version.mod_name.."    ".."最新版本："..mod_with_invalid_version.version

                                local title = modname == "shroomcake" and "蘑菇慕斯 󰀜 测试版" or "蘑菇慕斯 󰀜 智能模组"
                                local pds = PDS(title, content, {{
                                    text = "喊服主更新",
                                    cb = function()
                                        GLOBAL.TheFrontEnd:PopScreen()
                                        GLOBAL.TheNet:ServerModsDownloadCompleted(false, version_mismatch_msg, "SERVER_MODS_WORKSHOP_VERSION_MISMATCH" )
                                    end
                                },{
                                    text = "强制加入",
                                    cb = function()
                                        GLOBAL.TheFrontEnd:PopScreen()
                                        enable_server_mods()
                                        GLOBAL.TheNet:ServerModsDownloadCompleted(true, "", "")
                                    end
                                } 
                            }, nil, "big", "dark_wide")
                                pds.dialog.body:EnableWordWrap(false)
                                GLOBAL.TheFrontEnd:PushScreen(pds)
                            end
                        else
                            local sku = ""
                            if GLOBAL.IsRail() then
                                sku = "_RAIL"
                            end
                            if msg == "Access to mod denied" then
                                GLOBAL.TheNet:ServerModsDownloadCompleted(false, msg, "SERVER_MODS_WORKSHOP_ACCESS_DENIED"..sku)
                            else
                                GLOBAL.TheNet:ServerModsDownloadCompleted(false, msg, "SERVER_MODS_WORKSHOP_FAILURE"..sku)
                            end
                        end
                    end
                )
            else
                local error = "SERVER_MODS_NOT_ON_WORKSHOP"
                if GLOBAL.IsRail() then
                    error = "SERVER_MODS_NOT_ON_WORKSHOP_RAIL"
                end
                GLOBAL.TheNet:ServerModsDownloadCompleted(false, "You don't have the required mods to play on this server and they don't exist on the Workshop. You will need to download them manually.", error )
            end
        end
    else
        GLOBAL.TheNet:ServerModsDownloadCompleted(true, "", "")
    end
end